import Vue from 'vue';
import store from '@/store';
import Router from 'vue-router';
import routes from './router';
// import Cookies from 'js-cookie'

Vue.use(Router);

const router = new Router({
  scrollBehavior: () => ({
    y: 0
  }),
  routes
});
// 判断是否有权利
function auth (data) {
  var ret = true;
  // console.log(data)
  if (data === 'login' || data === 'home' || data === '首页') {
    return true;
  }
  // if(!store.state.app.token){
  //   store.commit('LOGOUT')
  //   return ret
  // }  
  // console.log(store.state.app.menu)
  for (var e in store.state.app.menu) {
    if (data === store.state.app.menu[e].Name) {
      // console.log(data)
      ret = true;
      break;
      // return true
    }
  }

  // console.log(ret)
  return ret;
}
router.beforeEach((to, from, next) => {
  if (document.documentElement.clientWidth <= 600) {
    store.commit('CLOSE_SLIDEBAR');
  }
  if (auth(to.name) || !from.name) {
    // console.log(store)
    // store.dispatch('getCurrentPageName', to.name)
    next();
  } else {
    next({
      name: 'login'
      // query: { redirect: to.fullPath }
    });
  }
  // if (to.meta.requiresAuth) {
  //   // let token = store.app.state.token
  //   if (store.app.state.token) {
  //     store.dispatch('getCurrentPageName', to.name)
  //     next()
  //   } else {
  //     next({
  //       path: '/login',
  //       query: {redirect: to.fullPath}
  //     })
  //   }
  // } else {
  //   store.dispatch('getCurrentPageName', to.name)
  //   next()
  // }
});

export default router;
